package com.lanyam.travel.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanyam.travel.base.dao.RoleMapper;
import com.lanyam.travel.base.dao.RoleMenuMapper;
import com.lanyam.travel.base.dao.RolePermissionMapper;
import com.lanyam.travel.base.dao.TicketTypeMapper;
import com.lanyam.travel.base.entity.Permission;
import com.lanyam.travel.base.entity.Role;
import com.lanyam.travel.base.entity.RoleMenu;
import com.lanyam.travel.base.entity.RolePermission;
import com.lanyam.travel.base.entity.TicketType;
import com.lanyam.travel.base.entity.TravelMenu;
import com.lanyam.travel.base.service.IRolePermissionService;
import com.lanyam.travel.base.service.IRoleService;
import com.lanyam.travel.base.service.ITicketTypeService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rosson
 * @since 2020-02-12
 */
@Service
public class RoleServiceImpl implements IRoleService {
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private RolePermissionMapper rolePermissionMapper;
	
	@Autowired
	private RoleMenuMapper roleMenuMapper;

	@Override
	public void addRole(Role role) {
		// TODO Auto-generated method stub
		roleMapper.insert(role);
	}

	@Override
	public List<Role> getAllRole() {
		// TODO Auto-generated method stub
		QueryWrapper<Role> wrapper = new QueryWrapper<Role>();
		wrapper.lt("power", 99);
		List<Role> allRole = roleMapper.selectList(wrapper);
//		List<Role> allRole = roleMapper.selectList(null);
		return allRole;
	}

	@Override
	public void update(Role role) {
		// TODO Auto-generated method stub
		Role r = roleMapper.selectById(role.getId());
		role.setPower(r.getPower());
		roleMapper.updateById(role);
		String roleId = role.getId();
		//更新角色权限
		List<Integer> permissions = role.getPermissionIds();
		if(null != permissions && permissions.size()>0)
		{
			saveRolePermission(roleId,permissions);
		}
		//更新角色菜单
		List<String> menuIds = role.getMenuIds();
		if(null != menuIds && menuIds.size()>0)
		{
			saveRoleMenu(roleId, menuIds);
		}
	}

	@Override
	public void deleteRole(String id) {
		// TODO Auto-generated method stub
		roleMapper.deleteById(id);
		//同步删除角色上的权限
		QueryWrapper<RolePermission> wrapper = new QueryWrapper<RolePermission>();
		wrapper.eq("role_id", id);
		rolePermissionMapper.delete(wrapper);
		//同步删除角色上的菜单
		QueryWrapper<RoleMenu> menuWrapper = new QueryWrapper<RoleMenu>();
		menuWrapper.eq("role_id", id);
		roleMenuMapper.delete(menuWrapper);
	}

	@Override
	public Role getRoleById(String id) {
		// TODO Auto-generated method stub
		Role role = roleMapper.selectById(id);
		List<Permission> permissionsInRole = rolePermissionMapper.selectPermissionsByRoleId(id);
		role.setPermissionInRole(permissionsInRole);
		List<TravelMenu> menuInRole = roleMenuMapper.selectMenusByRoleId(id);
		role.setMenuInRole(menuInRole);
		return role;
	}

	@Override
	public void saveRolePermission(String roleId, List<Integer> permissionIds) {
		// TODO Auto-generated method stub
		QueryWrapper<RolePermission> wrapper = new QueryWrapper<RolePermission>();
		wrapper.eq("role_id", roleId);
		rolePermissionMapper.delete(wrapper);
		RolePermission rolePermission;
		for (Integer permissionId : permissionIds)
		{
			rolePermission = new RolePermission();
			rolePermission.setRoleId(roleId);;
			rolePermission.setPermissionId(permissionId);
			rolePermissionMapper.insert(rolePermission);
		}
	}

	@Override
	public List<Permission> getPermissionsByRoleId(String roleId) {
		// TODO Auto-generated method stub
		List<Permission> selectList = rolePermissionMapper.selectPermissionsByRoleId(roleId);
		return selectList;
	}

	@Override
	public void batchInsertRolePermission(List<RolePermission> list) {
		// TODO Auto-generated method stub
		for (RolePermission rolePermission : list) {
			rolePermissionMapper.insert(rolePermission);
		}
	}

	@Override
	public void saveRoleMenu(String roleId, List<String> menuIds) {
		// TODO Auto-generated method stub
		QueryWrapper<RoleMenu> wrapper = new QueryWrapper<RoleMenu>();
		wrapper.eq("role_id", roleId);
		roleMenuMapper.delete(wrapper);
		RoleMenu roleMenu;
		if(null != menuIds && menuIds.size() > 0)
		{
			for (String menuId : menuIds)
			{
				roleMenu = new RoleMenu();
				roleMenu.setRoleId(roleId);;
				roleMenu.setMenuId(menuId);
				roleMenuMapper.insert(roleMenu);
			}
		}
	}

	@Override
	public void batchInsertRoleMenu(List<RoleMenu> list) {
		// TODO Auto-generated method stub
		for (RoleMenu roleMenu : list) {
			roleMenuMapper.insert(roleMenu);
		}
	}


}
